#include "api_include.h"

hard_t g_hard;

void api_hard_init(void)
{
	memset(&g_hard, 0, sizeof(g_hard));
}

void api_hard_read(void)
{
	static uint8_t btn_cold_cnt = 0;
	static uint8_t btn_save_cnt = 0;
	g_hard.key1 = IO_KEY_GET(IO_KEY1_PORT, IO_KEY1_PIN);
	g_hard.key2 = IO_KEY_GET(IO_KEY2_PORT, IO_KEY2_PIN);
	g_hard.key3 = IO_KEY_GET(IO_KEY3_PORT, IO_KEY3_PIN);
	g_hard.key4 = IO_KEY_GET(IO_KEY4_PORT, IO_KEY4_PIN);
	g_hard.key5 = IO_KEY_GET(IO_KEY5_PORT, IO_KEY5_PIN);
	if(IO_BTN_GET(IO_BTN_COLD_PORT, IO_BTN_COLD_PIN))
	{
		btn_cold_cnt++;
		if(btn_cold_cnt >= 200)
		{
			btn_cold_cnt = 200;
		}
	}
	else
	{
		btn_cold_cnt = 0;
	}
	g_hard.btn_cold = btn_cold_cnt>=200?1:0;
	if(IO_BTN_GET(IO_BTN_SAVE_PORT, IO_BTN_SAVE_PIN))
	{
		btn_save_cnt++;
		if(btn_save_cnt >= 200)
		{
			btn_save_cnt = 200;
		}
	}
	else
	{
		btn_save_cnt = 0;
	}
	g_hard.btn_save = btn_save_cnt>=200?1:0;
	g_hard.pir = IO_PIR_GET();
}

void api_hard_load(void)
{
	MCU_LED(g_hard.mcu_led);		/* LED控制 */
	BEEP(g_hard.beep);
	COLD_IND(g_hard.ind_cold);
	SAVE_IND(g_hard.ind_save);
	ERR_LED(g_hard.led_err);
	COLD_RELAY(g_hard.relay_cold);
	bsp_pwm_set_cmp(PWM_CHANNEL1, g_hard.light_warm);
	bsp_pwm_set_cmp(PWM_CHANNEL2, g_hard.light_cold);
}
